#include "socket.hpp"
#include "header.h"
#include "logger.h"
namespace moqi
{
Socket::Socket()
{
    _fd = socket(AF_INET, SOCK_STREAM, 0);
    if(_fd < 0)
    {
        perror("Socket");
    }
}

Socket::Socket(int fd):
_fd(fd)
{

}

Socket::~Socket()
{
    close(_fd);
}

int Socket::fd()
{
    return _fd;
}

void Socket::shutdownWrite()
{
    ::shutdown(_fd, SHUT_WR);
}

void Socket::setNonblock()//设置为非阻塞
{
    int flags = fcntl(_fd, F_GETFL, 0);
    if (flags == -1) {
        LOG_ERROR("fcntl F_GETFL failed");
        throw std::system_error(errno, std::system_category(), "fcntl F_GETFL failed");
    }
    
    if (fcntl(_fd, F_SETFL, flags | O_NONBLOCK) == -1) {
        LOG_ERROR("fcntl F_GETFL failed");
        throw std::system_error(errno, std::system_category(), "fcntl F_SETFL failed");
    }
}

}
